package de.gameduell.framework.application {
	import de.gameduell.framework.asset.AssetGroup;
	import de.gameduell.framework.asset.SWFAsset;
	import de.gameduell.framework.debug.Debug;
	import de.gameduell.framework.game.watch.SingleTime;
	import de.gameduell.framework.gui.popup.PopupManager;
	import de.gameduell.framework.gui.util.ToolTip;
	import de.gameduell.framework.locale.Locale;
	import de.gameduell.framework.util.Audio;
	import de.gameduell.framework.util.Library;
	
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.Event;	

	import de.gameduell.framework.util.Tools;
	import de.gameduell.framework.asset.XMLAsset;

	/**
	 * A basic document class which should be the base of every GameDuell flash application.
	 * Provides all basic features to set up a simple application with
	 * popups, audio and internationalization/localization abilities.
	 * Usually you don't use this class directly. To setup a application
	 * with already holds communication features you should use the <b>OnlineApplication</b>
	 * class instead or even the more spezialized classes like <b>Game</b>, <b>SingleplayerGame</b>,
	 * <b>MultiplayerGame</b>.
	 * 
	 * @see OnlineApplication
 	 * @see	Game
	 * @see SingleplayerGame
	 * @see MultiplayerGame
	 */
	public class Application extends Sprite implements IApplication {

		private const LANG:String 	 	   = "lang";
		private const GENERAL_LANG:String  = "general_lang";
		
		protected var library:Library;
		protected var assets:AssetGroup;
		protected var settings:Settings;
		protected var timeline:Stage;
		protected var audio:Audio;
		protected var popupManager:PopupManager;
		protected var window:Window;
		protected var locale:Locale;
		protected var isStandalone:Boolean;
		protected var isConnected:Boolean;
		protected var baseURL:String;
		protected var singleTime:SingleTime;

		/**  
		 * @param aStage    = the stage of the application 
		 * @param aAssets   = comprises the assets from the configfile
		 * @param aSettings = comprises the settings from the configfile
		 * @param isOnline  = is the application online or standalone
		**/	
		public function init(aStage:Stage, aAssets:AssetGroup, aSettings:Settings, standalone:Boolean):void {
			try {
				singleTime = SingleTime.getInstance();
				singleTime.start();
				library   = Library.getInstance();
				var graphicsAsset:SWFAsset = SWFAsset(aAssets.getAssetById("graphics"));
				var soundsAsset:SWFAsset   = SWFAsset(aAssets.getAssetById("sounds"));
				if(graphicsAsset != null){		
					library.graphicsDomain = graphicsAsset.applicationDomain;
				}
				if(soundsAsset != null){	
					library.soundsDomain = soundsAsset.applicationDomain;	
				}		
				audio     = Audio.getInstance();
				timeline  = aStage;
				assets    = aAssets;
				settings  = aSettings;	
				isStandalone = standalone;
				isConnected  = false;
				
				timeline.frameRate = settings.framerate;
				
				var lang:XML = XMLAsset(assets.getAssetById(LANG)).xmlData;	
				var generalLang:XML = settings.useGeneralLangFile ? XMLAsset(assets.getAssetById(GENERAL_LANG)).xmlData : new XML();
				
				locale = Locale.getInstance();	
				locale.init(aStage,lang, generalLang);
							
				window = new Window(library.createSprite("window"));
				ToolTip.init(window.sprite);
				// put it below the loader screen				
				timeline.addChildAt(window.displayObjectContainer,0);
				
				popupManager = PopupManager.getInstance();
				popupManager.init(window);				
 				
				baseURL = root.loaderInfo.url;
				baseURL = baseURL.substr( 0, baseURL.indexOf( "?" ) );
				baseURL = baseURL.substr( 0, baseURL.lastIndexOf( "/" ) + 1 );
				
			}catch(e:Error) {
				Debug.trace("Application::init: ERROR " + e.getStackTrace(), Debug.ERROR);
				Tools.traceToFrontend("Error initializing application");
			}		
		}
		
		/**  
		 * Removes the preloader sprite and switching alignment and scaleMode of the stage.
		 * 
		 * @param event		can also be used as eventHandler 
		 **/	
		protected function removePreloaderScreen(event:Event = null):void {
			try{
				timeline.scaleMode = settings.scaleMode;
				timeline.align     = settings.stageAlign;
				timeline.removeChildAt(timeline.numChildren - 1);
			}catch(e:Error){
				Debug.trace("Application::removePreloaderScreen: ERROR " + e);
			}
		}
	}
}
